package scparser;

import java.awt.Color;
import java.io.IOException;
import javax.vecmath.Point3d;

import scparser.factory.primitiveFactory.FactoryException;

public class ParserHelper {
	public static double fov = 60.0;		
	
	public static Double takeDouble(String line, String header) throws IOException{
		line=line.substring(line.indexOf(header)  + header.length() + 1);
		return Double.valueOf(line);
	}
	
	public static Point3d takePoint(String line, String header) throws IOException{
		String point = line;
		if(!point.contains(header)) {
			throw new IllegalArgumentException(line);
		}
		
		point=point.substring(point.indexOf(header) + header.length() + 1 );
		String [] c = point.trim().split(" ");
		
		return new Point3d(Double.valueOf(c[0]), Double.valueOf(c[1]), Double.valueOf(c[2]));
		
	}
	
	public static String takeString( String line, String header) throws IOException{
		String name = line;
		name = name.substring(name.indexOf(header) + header.length());
		return name.trim();
	}
	
	public static Color getColor(String line) throws FactoryException, IOException {
		line = line.replaceAll("\\{", "");
		line = line.replaceAll("\\}", "");
		String[] values = line.trim().split(" ");
		String type = values [0] + values[1];
		float r, g, b;	// rgb color
		
		if (type == null || type.equals("")) {
			throw new FactoryException("shader type option missing");
		}
		Point3d p;
		
		String nonLinear = "\"sRGB nonlinear\"";
		//String linear = "\"sRGB linear\"";
		
		if (line.contains(nonLinear)) {
			p = ParserHelper.takePoint(line, nonLinear);
		} else {
			p = ParserHelper.takePoint(line, "\"sRGB linear\"");
		}
		
		
		// obtiene los componentes RGB del color
		r = (float) p.x;
		g = (float) p.y;
		b = (float) p.z;
		
		return new Color(r, g, b);
		
	}

}
